#####################
#Replication of table 6 in the Appendix #
#######################
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1);
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
#
dat.1 <- read.dta("useWAY1_rep.dta")#This is Jo+Gartke+ Singh and Way#
#
origin <- rep(NA, nrow(dat.1))#
dat <- as.data.frame(cbind(dat.1,origin))#
#
for(i in 1:length(unique(dat$ccode))){#
	tmp <- subset(dat, dat$ccode == unique(dat$ccode)[i])#
	dat$origin[which(dat$ccode == unique(dat$ccode)[i])] <- tmp$start[1]#
}#
#
dat.1 <- dat#
#
dat.1$start <- dat.1$start - dat.1$origin#
dat.1$stop <- dat.1$stop - dat.1$origin#
dat.1$t <- (dat.1$stop - dat.1$start)#
dat.1$t0 <- 0
#####################
#Replication of table 6 in the Appendix #
#######################
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1)
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
#####################
#Replication of table 6 in the Appendix #
#######################
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1)
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
dat.1 <- read.dta("useWAY1Ex_rep.dta")#This is Jo+Gartke+ Singh and Way#
#
origin <- rep(NA, nrow(dat.1))#
dat <- as.data.frame(cbind(dat.1,origin))#
#
for(i in 1:length(unique(dat$ccode))){#
	tmp <- subset(dat, dat$ccode == unique(dat$ccode)[i])#
	dat$origin[which(dat$ccode == unique(dat$ccode)[i])] <- tmp$start[1]#
}#
#
dat.1 <- dat#
#
dat.1$start <- dat.1$start - dat.1$origin#
dat.1$stop <- dat.1$stop - dat.1$origin#
#
dat.1$t <- (dat.1$stop - dat.1$start)#
dat.1$t0 <- 0#
#
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
#
mod.2 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.2)
##########################################
######## Time Varying Data Test ##########
##########################################
#
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
dat.1 <- read.dta("usetv_npt_REP.dta")
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
dat.1 <- read.dta("usetv_REP.dta")
################
#### Table 17####
#
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
dat.1$npt.lt.1 <- dat.1$npt_effC_1 * log(dat.1$t + 1)#
dat.1$mid.lt.2 <- dat.1$lmid_past5yrs_2 * log(dat.1$t + 1)#
dat.1$nuk.lt.2 <- dat.1$nuk7set1_2 * log(dat.1$t + 1)#
dat.1$npt.lt.2 <- dat.1$npt_effC_2* log(dat.1$t + 1)#
dat.1$neo.lt.2 <- dat.1$neopatr_2 * log(dat.1$t + 1)#
mod.1 <- coxph(Surv(t0, t, status) ~ #
nuk7set1_1 + npt_effC_1 + lmid_past5yrs_1 + neopatr_1	+ mid.lt.1+#
nuk7set1_2 + npt_effC_2 + lmid_past5yrs_2 + neopatr_2 	+ mid.lt.2 + npt.lt.2 + nuk.lt.2+#
nuk7set1_3  +  npt_effC_3 + lmid_past5yrs_3 + neopatr_3 +#
strata(trans) + cluster(ccode), data = dat.1, method = "efron")	#
summary(mod.1)
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
dat.1$npt.lt.1 <- dat.1$npt_effC_1 * log(dat.1$t + 1)#
dat.1$mid.lt.2 <- dat.1$lmid_past5yrs_2 * log(dat.1$t + 1)#
dat.1$nuk.lt.2 <- dat.1$nuk7set1_2 * log(dat.1$t + 1)#
dat.1$npt.lt.2 <- dat.1$npt_effC_2* log(dat.1$t + 1)#
dat.1$neo.lt.2 <- dat.1$neopatr_2 * log(dat.1$t + 1)
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)
dat.1$npt.lt.1 <- dat.1$npt_effC_1 * log(dat.1$t + 1)
rm(list=ls())
library(foreign)#
library(survival)#
library(mstate)
dat.1 <- read.dta("use_rep.dta")
dat.1 <- subset(dat.1, dat.1$ccode != 560)
tmat <- transMat(list(c(2), #transitions from no program#
			c(1, 3), 		#transitions from program#
			c()),			#transition from weapon			#
			names = c("no_program", "program", "weapon"))#
#
##### Convert Data Type#
attr(dat.1, "trans") <- tmat#
class(dat.1) <- c("msdata", "data.frame")#
#
#Check number of transitions is correct#
events(dat.1)
dat.1$from
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)
dat.1 <- read.dta("use_rep.dta")
dat.1 <- subset(dat.1, dat.1$ccode != 560)
######################################
##### Prepare Transition Matrix ######
######################################
#
tmat <- transMat(list(c(2), #transitions from no program#
			c(1, 3), 		#transitions from program#
			c()),			#transition from weapon			#
			names = c("no_program", "program", "weapon"))#
#
##### Convert Data Type#
attr(dat.1, "trans") <- tmat#
class(dat.1) <- c("msdata", "data.frame")
#Check number of transitions is correct#
events(dat.1)#
table(dat$status, dat$to, dat$from)
#Replicate Table 2 in Main Text#
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1)
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
#
dat.1 <- read.dta("useWAY1_rep.dta")#This is Jo+Gartke+ Singh and Way#
#
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1)
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
#
dat.1 <- read.dta("useWAY1_rep.dta")#This is Jo+Gartke+ Singh and Way#
#
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
#
mod.1 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.1)
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
#
dat.1 <- read.dta("useWAY1Ex_rep.dta")#This is Jo+Gartke+ Singh and Way#
#
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
#
mod.2 <- coxph(Surv(t, status) ~ #
		nuk7set1_1 + npt_eff_1+   lmid_past5yrs_1 +  neopatr_1 +mid.lt.1+#
		nuk7set1_2 + npt_eff_2+   lmid_past5yrs_2+ neopatr_2+#
		nuk7set1_3 + npt_eff_3 +  lmid_past5yrs_3+ neopatr_3#
				+ strata(trans), data = dat.1, method = "breslow")#
summary(mod.2)
##########################################
######## Time Varying Data Test ##########
##########################################
#
rm(list=ls())#
library(foreign)#
library(survival)#
library(mstate)#
dat.1 <- read.dta("usetv_npt_REP.dta")
dat.1$mid.lt.1 <- dat.1$lmid_past5yrs_1 * log(dat.1$t + 1)#
dat.1$npt.lt.1 <- dat.1$npt_effC_1 * log(dat.1$t + 1)#
dat.1$mid.lt.2 <- dat.1$lmid_past5yrs_2 * log(dat.1$t + 1)#
dat.1$nuk.lt.2 <- dat.1$nuk7set1_2 * log(dat.1$t + 1)#
dat.1$npt.lt.2 <- dat.1$npt_effC_2* log(dat.1$t + 1)#
dat.1$neo.lt.2 <- dat.1$neopatr_2 * log(dat.1$t + 1)
mod.1 <- coxph(Surv(t0, t, status) ~ #
nuk7set1_1 + npt_effC_1 + lmid_past5yrs_1 + neopatr_1	+ mid.lt.1+#
nuk7set1_2 + npt_effC_2 + lmid_past5yrs_2 + neopatr_2 	+ mid.lt.2 + npt.lt.2 + nuk.lt.2+#
nuk7set1_3  +  npt_effC_3 + lmid_past5yrs_3 + neopatr_3 +#
strata(trans) + cluster(ccode), data = dat.1, method = "efron")	#
summary(mod.1)
